package com.jkl.service.home;

import com.jkl.dao.home.MainIndexChangMapper;
import com.jkl.entity.home.HomeRequestEntity;
import com.jkl.entity.home.MainIndexMappingEntity;
import com.jkl.entity.home.MainIndexResponseEntity;
import com.jkl.service.home.mainIndexInter.MainIndexChangService;
import com.jkl.untils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zjj
 * @date 2019/12/23
 * @Description ${描述一个类的相关信息}
 */
@Service
public class MainIndexServiceChangImpl implements MainIndexChangService {

    @Autowired
    private MainIndexChangMapper mainIndexChangDao;


    /**
     * @参数
     * @返回值 判断时间条件 组装时间数据
     */
    private List<Map<String, Object>> timeCondtion(HomeRequestEntity homeRequestEntity) {
        if (StringUtils.isEmpty(homeRequestEntity.getEndTime()) && StringUtils.isEmpty(homeRequestEntity.getStartTime())) {
            //获取最近一年开始时间和结束时间不包括本月
            String startTime = LocalDate.now().minusYears(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
            String endTime = LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
            if(startTime.compareTo("2019-09")<0){
                homeRequestEntity.setEndTime("2020-08");
                homeRequestEntity.setStartTime("2019-09");
            }else{
                homeRequestEntity.setEndTime(endTime);
                homeRequestEntity.setStartTime(startTime);
            }
        }
        //获取时间段月份
        List<String> montListByTime = DateUtil.getMontListByTime(homeRequestEntity.getStartTime(), homeRequestEntity.getEndTime());
        //组装时间段
        List<Map<String, Object>> monthlistMap = new ArrayList<>();
        for (int i = 0; i < montListByTime.size(); i++) {
            Map mapTime = new HashMap();
            mapTime.put(montListByTime.get(i), 0);
            monthlistMap.add(mapTime);
        }
        return monthlistMap;
    }

    /**
     * @参数
     * @返回值 处理总量分析
     */
    @Override
    public MainIndexResponseEntity getProductionAmountAnalysis(HomeRequestEntity homeRequestEntity) {
        List<Map<String, Object>> monthlistMap = timeCondtion(homeRequestEntity);
        //获取数据
        List<MainIndexMappingEntity> productionAmountAnalysis = mainIndexChangDao.getProductionAmountAnalysis(homeRequestEntity);

        List<String> yearArray = new ArrayList<>();
        List<Double> disposeTotalArray = new ArrayList<>();
        for (Map<String, Object> map : monthlistMap) {
            String key = map.keySet().iterator().next();
            for (MainIndexMappingEntity amountAnalysis : productionAmountAnalysis) {
                if (amountAnalysis.getTime().equals(key)) {
                    //计算处理总量
                    double value = (new BigDecimal(amountAnalysis.getDisposedTotal() != null ? amountAnalysis.getDisposedTotal() : 0).divide(new BigDecimal(10000))).setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue();
                    map.put(key, value);
                    break;
                }
            }
            disposeTotalArray.add(Double.parseDouble(map.get(key).toString()));
            yearArray.add(key);
        }

        MainIndexResponseEntity responseEntity = new MainIndexResponseEntity();
        responseEntity.setDisposeTotalArray(disposeTotalArray);
        responseEntity.setYearArray(yearArray);
        return responseEntity;
    }

    /**
     * @参数
     * @返回值 处置量分析
     */
    @Override
    public MainIndexResponseEntity disposalAnalysis(HomeRequestEntity homeRequestEntity) {
        List<Map<String, Object>> monthlist = timeCondtion(homeRequestEntity);
        //获取有害垃圾集合
        List<MainIndexMappingEntity> harmGarbageList = mainIndexChangDao.harmGarbageList(homeRequestEntity);
        //获取易腐垃圾集合
        List<MainIndexMappingEntity> perishableGarbageList = mainIndexChangDao.perishableGarbageList(homeRequestEntity);
        //其他垃圾集合
        List<MainIndexMappingEntity> otherGarbageList = mainIndexChangDao.otherGarbageList(homeRequestEntity);

        List<String> yearArray = new ArrayList<>();
        List<Double> harmGarbageArray = new ArrayList<>();
        List<Double> perishableGarbageArray = new ArrayList<>();
        List<Double> otherGarbageArray = new ArrayList<>();

        Map<String, Object> perishableGarbageMap = new HashMap<>();
        Map<String, Object> otherGarbageMap = new HashMap<>();
        for (Map<String, Object> map : monthlist) {
            String time = map.keySet().iterator().next();
            perishableGarbageMap.put(time, 0);
            otherGarbageMap.put(time, 0);
            for (MainIndexMappingEntity mainIndexMappingEntity : harmGarbageList) {
                if (mainIndexMappingEntity.getTime().equals(time)) {
                    //处理有害垃圾产生量
                    double value = (new BigDecimal(mainIndexMappingEntity.getHarmGarbageTotal() != null ? mainIndexMappingEntity.getHarmGarbageTotal() : 0)).setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue();
                    map.put(time, value);
                    break;
                }
            }
            for (MainIndexMappingEntity mainIndexMappingEntity : perishableGarbageList) {
                if (mainIndexMappingEntity.getTime().equals(time)) {
                    //易腐垃圾产生量
                    double value = (new BigDecimal(mainIndexMappingEntity.getPerishableGarbageTotal() != null ? mainIndexMappingEntity.getPerishableGarbageTotal() : 0).divide(new BigDecimal(10000))).setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue();
                    perishableGarbageMap.put(time, value);
                    break;
                }
            }
            for (MainIndexMappingEntity mainIndexMappingEntity : otherGarbageList) {
                if (mainIndexMappingEntity.getTime().equals(time)) {
                    //其他垃圾产生量
                    double value = (new BigDecimal(mainIndexMappingEntity.getOtherGarbageTotal() != null ? mainIndexMappingEntity.getOtherGarbageTotal() : 0).divide(new BigDecimal(10000))).setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue();
                    otherGarbageMap.put(time, value);
                    break;
                }
            }
            otherGarbageArray.add(Double.parseDouble(otherGarbageMap.get(time).toString()));
            perishableGarbageArray.add(Double.parseDouble(perishableGarbageMap.get(time).toString()));
            harmGarbageArray.add(Double.parseDouble(map.get(time).toString()));
            yearArray.add(time);

        }

        MainIndexResponseEntity mainIndexResponseEntity = new MainIndexResponseEntity();
        mainIndexResponseEntity.setYearArray(yearArray);
        mainIndexResponseEntity.setHarmGarbageArray(harmGarbageArray);
        mainIndexResponseEntity.setPerishableGarbageArray(perishableGarbageArray);
        mainIndexResponseEntity.setOtherGarbageArray(otherGarbageArray);
        return mainIndexResponseEntity;
    }

    /**
     * @参数
     * @返回值 回收利用折线图
     */
    @Override
    public MainIndexResponseEntity recyclePolyline(HomeRequestEntity homeRequestEntity) {
        List<Map<String, Object>> monthListMap = timeCondtion(homeRequestEntity);
        List<String> yearArray = new ArrayList<>();
        List<Double> recycleArray = new ArrayList<>();
        List<MainIndexMappingEntity> mainIndexMappingEntities = mainIndexChangDao.recyclePolyline(homeRequestEntity);
        for (Map<String, Object> map : monthListMap) {
            String time = map.keySet().iterator().next();
            for (MainIndexMappingEntity mainIndexMappingEntity : mainIndexMappingEntities) {
                if (mainIndexMappingEntity.getTime().equals(time)) {
                    //回收利用
                    double value = (new BigDecimal(mainIndexMappingEntity.getRecycleTotal() != null ? mainIndexMappingEntity.getRecycleTotal() : 0)).setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue();
                    map.put(time, value);
                    break;
                }
            }
            recycleArray.add(Double.parseDouble(map.get(time).toString()));
            yearArray.add(time);

        }
        MainIndexResponseEntity mainIndexResponseEntity = new MainIndexResponseEntity();
        mainIndexResponseEntity.setYearArray(yearArray);
        mainIndexResponseEntity.setRecycleArray(recycleArray);
        return mainIndexResponseEntity;
    }

    /**
     * @参数
     * @返回值 回收利用饼状图
     */
    @Override
    public MainIndexResponseEntity recyclepieChart(HomeRequestEntity homeRequestEntity) {
        //获取当年和上个月时间点
        String nowyear = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy"));
        String lastmonth = LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
        if (homeRequestEntity.getType() != null && "0".equals(homeRequestEntity.getType())) {
            homeRequestEntity.setTime(nowyear);
        } else {
            homeRequestEntity.setTime(lastmonth);
        }
        //查询饼状图
        MainIndexMappingEntity mainIndexMappingEntity = mainIndexChangDao.recyclepieChart(homeRequestEntity);

        //设置当年回收量或者上月回收量查询条件
        if (homeRequestEntity.getType() != null && "0".equals(homeRequestEntity.getType())) {
            homeRequestEntity.setTime(lastmonth);
        } else {
            homeRequestEntity.setTime(nowyear);
        }
        //获取当年回收量或者上月回收量
        MainIndexMappingEntity mainIndexMappingEntity1 = mainIndexChangDao.recycleTotal(homeRequestEntity);

        MainIndexResponseEntity mainIndexResponseEntity = new MainIndexResponseEntity();
        if (mainIndexMappingEntity!=null) {
            mainIndexResponseEntity.setPaperTotal(new BigDecimal(mainIndexMappingEntity.getPaperTotal() != null ? mainIndexMappingEntity.getPaperTotal() : 0).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            mainIndexResponseEntity.setGlassTotal(new BigDecimal(mainIndexMappingEntity.getGlassTotal() != null ? mainIndexMappingEntity.getGlassTotal() : 0).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            mainIndexResponseEntity.setMetalTotal(new BigDecimal(mainIndexMappingEntity.getMetalTotal() != null ? mainIndexMappingEntity.getMetalTotal() : 0).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            mainIndexResponseEntity.setPlasticTotal(new BigDecimal(mainIndexMappingEntity.getPlasticTotal() != null ? mainIndexMappingEntity.getPlasticTotal() : 0).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            mainIndexResponseEntity.setTextile(new BigDecimal(mainIndexMappingEntity.getTextile() != null ? mainIndexMappingEntity.getTextile() : 0).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            mainIndexResponseEntity.setOther(new BigDecimal(mainIndexMappingEntity.getOther() != null ? mainIndexMappingEntity.getOther() : 0).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            mainIndexResponseEntity.setRecycleTotal(new BigDecimal(mainIndexMappingEntity.getRecycleTotal() != null ? mainIndexMappingEntity.getRecycleTotal() : 0).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        }else {
            mainIndexResponseEntity.setPaperTotal(0d);
            mainIndexResponseEntity.setGlassTotal(0d);
            mainIndexResponseEntity.setMetalTotal(0d);
            mainIndexResponseEntity.setPlasticTotal(0d);
            mainIndexResponseEntity.setTextile(0d);
            mainIndexResponseEntity.setOther(0d);
            mainIndexResponseEntity.setRecycleTotal(0d);
        }
        if (mainIndexMappingEntity1!=null) {
            mainIndexResponseEntity.setRecycleMonthOrYearTotal(new BigDecimal(mainIndexMappingEntity1.getRecycleTotal() != null ? mainIndexMappingEntity1.getRecycleTotal() : 0).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        }else {
            mainIndexResponseEntity.setRecycleMonthOrYearTotal(0d);
        }
        return mainIndexResponseEntity;
    }

    /**
     * @参数
     * @返回值 资源化分析
     */
    @Override
    public MainIndexResponseEntity getResourceeAnalysis(HomeRequestEntity homeRequestEntity) {
        if (StringUtils.isEmpty(homeRequestEntity.getEndTime()) && StringUtils.isEmpty(homeRequestEntity.getStartTime())) {
            //获取最最近六个月不包括当前月
            String startTime = LocalDate.now().minusMonths(6).format(DateTimeFormatter.ofPattern("yyyy-MM"));
            String endTime = LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
            if(startTime.compareTo("2019-09")<0){
                homeRequestEntity.setEndTime("2020-02");
                homeRequestEntity.setStartTime("2019-09");
            }else{
                homeRequestEntity.setEndTime(endTime);
                homeRequestEntity.setStartTime(startTime);
            }
        }
        //获取时间列表
        List<Map<String, Object>> timeList = timeCondtion(homeRequestEntity);
        //焚烧发电
        List<MainIndexMappingEntity> incinerationPowerList = mainIndexChangDao.incinerationPowerList(homeRequestEntity);
        //f焚烧供热
        List<MainIndexMappingEntity> incinerationHotList = mainIndexChangDao.incinerationHotList(homeRequestEntity);
        //生物柴油
        List<MainIndexMappingEntity> biodieselList = mainIndexChangDao.biodieselList(homeRequestEntity);
        //沼气
        List<MainIndexMappingEntity> biogasList = mainIndexChangDao.biogasList(homeRequestEntity);
        List<String> yearArray = new ArrayList<>();
        List<Double> incinerationPowerArray = new ArrayList<>();
        List<Double> incinerationHotArray = new ArrayList<>();
        List<Double> biodieselArray = new ArrayList<>();
        List<Double> biogasArray = new ArrayList<>();
        Map<String, Object> incinerationHotMap = new HashMap<>();
        Map<String, Object> biodieselMap = new HashMap<>();
        Map<String, Object> biogasMap = new HashMap<>();
        for (Map<String, Object> map : timeList) {
            String time = map.keySet().iterator().next();
            incinerationHotMap.put(time, 0);
            biodieselMap.put(time, 0);
            biogasMap.put(time, 0);
            //焚烧发电集合
            for (MainIndexMappingEntity mainIndexMappingEntity : incinerationPowerList) {
                if (mainIndexMappingEntity.getTime().equals(time)) {
                    double value = new BigDecimal(mainIndexMappingEntity.getIncinerationPower() != null ? mainIndexMappingEntity.getIncinerationPower() : 0).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    map.put(time, value);
                    break;
                }
            }
            //焚烧供热
            for (MainIndexMappingEntity mainIndexMappingEntity : incinerationHotList) {
                if (mainIndexMappingEntity.getTime().equals(time)) {
                    double value = new BigDecimal(mainIndexMappingEntity.getIncinerationHot() != null ? mainIndexMappingEntity.getIncinerationHot() : 0).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    incinerationHotMap.put(time, value);
                    break;
                }
            }
            //生物柴油
            for (MainIndexMappingEntity mainIndexMappingEntity : biodieselList) {
                if (mainIndexMappingEntity.getTime().equals(time)) {
                    double value = new BigDecimal(mainIndexMappingEntity.getBiodiesel() != null ? mainIndexMappingEntity.getBiodiesel() : 0).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    biodieselMap.put(time, value);
                    break;
                }
            }
            //沼气
            for (MainIndexMappingEntity mainIndexMappingEntity : biogasList) {
                if (mainIndexMappingEntity.getTime().equals(time)) {
                    double value = new BigDecimal(mainIndexMappingEntity.getBiogas() != null ? mainIndexMappingEntity.getBiogas() : 0).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    biogasMap.put(time, value);
                    break;
                }
            }
            biogasArray.add(Double.parseDouble(biogasMap.get(time).toString()));
            biodieselArray.add(Double.parseDouble(biodieselMap.get(time).toString()));
            incinerationHotArray.add(Double.parseDouble(incinerationHotMap.get(time).toString()));
            incinerationPowerArray.add(Double.parseDouble(map.get(time).toString()));
            yearArray.add(time);
        }

        MainIndexResponseEntity mainIndexResponseEntity = new MainIndexResponseEntity();
        mainIndexResponseEntity.setIncinerationHotArray(incinerationHotArray);
        mainIndexResponseEntity.setIncinerationPowerArray(incinerationPowerArray);
        mainIndexResponseEntity.setBiodieselArray(biodieselArray);
        mainIndexResponseEntity.setBiogasArray(biogasArray);
        mainIndexResponseEntity.setYearArray(yearArray);
        return mainIndexResponseEntity;
    }

    /**
     * @参数
     * @返回值 GIS
     */
    @Override
    public MainIndexResponseEntity getGisAnalysis(HomeRequestEntity homeRequestEntity) {
        String nowMonth = LocalDate.now().minusMonths(2).format(DateTimeFormatter.ofPattern("yyyy-MM"));
        String nowYear = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy"))+"-01";
        String lastMonth = LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
        String nowTime = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM"));
        //上月减量
        homeRequestEntity.setTime(nowMonth);
        MainIndexMappingEntity lastMonthRedutionToal = mainIndexChangDao.gis(homeRequestEntity);
        //当月减量
        homeRequestEntity.setTime(lastMonth);
        MainIndexMappingEntity nowMonthRedutionToal = mainIndexChangDao.gis(homeRequestEntity);

        MainIndexResponseEntity responseEntity = new MainIndexResponseEntity();

        //计算减量化
        if (lastMonthRedutionToal != null && lastMonthRedutionToal.getReductionTotal()!=null) {
            if (lastMonthRedutionToal.getReductionTotal() == 0d) {
                responseEntity.setReductionRate(0d);
            }else {
                if (nowMonthRedutionToal != null && nowMonthRedutionToal.getReductionTotal() != null) {
                    //计算减量化
                    double value = (new BigDecimal(lastMonthRedutionToal.getReductionTotal()).subtract(new BigDecimal(nowMonthRedutionToal.getReductionTotal()))).divide(new BigDecimal(lastMonthRedutionToal.getReductionTotal()), 6, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
                    responseEntity.setReductionRate(value);
                }else {
                    responseEntity.setReductionRate(100d);
                }
            }
        }else {
            responseEntity.setReductionRate(0d);
        }
        //资源化
        if (nowYear.equals(nowTime)) {
            homeRequestEntity.setTime(null);
            String starTime = LocalDate.now().minusYears(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
            String endTimeTime = LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
            homeRequestEntity.setStartTime(starTime);
            homeRequestEntity.setEndTime(endTimeTime);
        }else{
            homeRequestEntity.setTime(null);
            homeRequestEntity.setStartTime(nowYear);
            homeRequestEntity.setEndTime(lastMonth);
        }
        MainIndexMappingEntity resourceTotal = mainIndexChangDao.gis(homeRequestEntity);
        if (resourceTotal!=null) {
            if(resourceTotal.getResourceDenominator()!=null||resourceTotal.getResourceDenominator()!=0d){
                int rate = new BigDecimal(resourceTotal.getResourceMolecule() != null ? resourceTotal.getResourceMolecule() : 0).divide(new BigDecimal(resourceTotal.getResourceDenominator()),4,BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
                responseEntity.setResourceRate(rate);
            }else {
                responseEntity.setResourceRate(0);
            }
        }else {
            responseEntity.setResourceRate(0);
        }
        return responseEntity;
    }
}
